package com.major.thread;

public class DeadLock {
    public static void main(String[] args) {

        MakeUp g1 = new MakeUp("g1");
        MakeUp g2 = new MakeUp("g2");

        Thread thread = new Thread(g1);
        Thread thread2 = new Thread(g2);

        thread.start();
        thread2.start();

    }


}

class Mirror {
}

class LipStick {
}

class MakeUp implements Runnable {

    static Mirror mirror = new Mirror();
    static LipStick lipStick = new LipStick();

    String name;

    public MakeUp(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void makeup() throws InterruptedException {
        if ("g1".equals(name)){
            synchronized (mirror){
                System.out.println(name+"得到镜子");
                Thread.sleep(0);
                synchronized (lipStick){
                    System.out.println(name+"得到口红");
                }
            }
        }else {
            synchronized (lipStick){
                System.out.println(name+"得到口红");
                Thread.sleep(0);
            }
            synchronized (mirror){
                System.out.println(name+"得到镜子");
            }
        }
    }
}
